TARGET = libtoxic.a
SRC_PATH = bits bits/posix net
INCLUDE_PATH = .
LIBRARY_PATH = 
LIBRARIES = 
CXX = g++
FLAGS = -Wall -g -fPIC
INCLUDE_PATH := $(addprefix -I,$(INCLUDE_PATH))
LIBRARY_PATH := $(addprefix -L,$(LIBRARY_PATH))
LIBRARIES := $(addprefix -l,$(LIBRARIES))
CXXFLAGS := $(FLAGS) $(INCLUDE_PATH) $(LIBRARY_PATH)
SRC := $(wildcard $(addsuffix /*.cpp,$(SRC_PATH)))
OBJECTS := $(patsubst %.cpp,%.o,$(SRC))
DEPEND := $(patsubst %.cpp,%.d,$(SRC))

all : $(TARGET) $(DEPEND)
	
$(filter %.so,$(TARGET)) : $(OBJECTS)
	$(CXX) -shared -fPIC -Wl,-soname,$@ -o $@ $(OBJECTS) $(LIBRARY_PATH) $(LIBRARIES)
	
$(filter %.a,$(TARGET)) : $(OBJECTS)
	ar -r $@ $(OBJECTS)

$(filter-out %.a %.so,$(TARGET)) : $(OBJECTS)
	$(CXX) -o $@ $(OBJECTS) $(LIBRARY_PATH) $(LIBRARIES)

.PRECIOUS : %.o
%.o : %.cpp %.d
	$(CXX) $(CXXFLAGS) -c $< -o $@

%.d : %.cpp
	@set -e;\
	$(CXX) -MM $(INCLUDE_PATH) $< > $@.$$$$;\
	sed 's,\($(*F)\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;\
	rm -f $@.$$$$;

-include $(DEPEND)

.PHONY : clean
clean :
	-rm -f $(TARGET) $(DEPEND) $(OBJECTS)
